package com.app.mingluxing.my;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.app.mingluxing.R;
import com.app.mingluxing.base.BaseActivity;
import com.app.mingluxing.bean.BabyDetailsResult;
import com.app.mingluxing.net.HttpUtil;
import com.app.mingluxing.utils.CommonUtils;
import com.app.mingluxing.utils.Constant;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2015/9/9.
 */
public class BabyModifyNameActivity extends BaseActivity{


    private EditText et_name;
    private String id;
    private ImageView iv_delete;
    private BabyDetailsResult.DataEntity babyInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_modify_name);
        id = getIntent().getStringExtra("id");

        babyInfo = getIntent().getParcelableExtra("babyInfo");

        initWidget();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mApplication.cancelPendingRequests(getClass().getName());
    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);

        final TextView tv_right = (TextView) findViewById(R.id.tv_right);

        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_right.setText(getResources().getString(R.string.done));
        tv_title.setText(getResources().getString(R.string.modify_name));
        et_name = (EditText) findViewById(R.id.et_name);

        iv_delete = (ImageView) findViewById(R.id.iv_delete);
//        et_name.setSelection(et_name.getText().toString().length());
        iv_delete.setOnClickListener(this);
        iv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);

        if (babyInfo != null)
            et_name.setText(babyInfo.childName);
        et_name.setSelection(et_name.getText().toString().trim().length());
        tv_right.setTextColor(getResources().getColor(R.color.line_gray));
        setClick(tv_right, false);

        et_name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    //显示叉号
                    iv_delete.setVisibility(et_name.getText().toString().trim().length() > 0 ? View.VISIBLE : View.INVISIBLE);

                    et_name.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                            //显示叉号
                            iv_delete.setVisibility(et_name.getText().toString().trim().length() > 0 ? View.VISIBLE : View.INVISIBLE);


                            if (babyInfo == null) {
                                tv_right.setTextColor(getResources().getColor(R.color.black));
                                setClick(tv_right, true);
                                return;
                            }

                            if (babyInfo.childName.equals(et_name.getText().toString())
                                    || CommonUtils.isNull(et_name.getText().toString().trim())) {

                                tv_right.setTextColor(getResources().getColor(R.color.line_gray));
                                setClick(tv_right, false);

                            } else {

                                tv_right.setTextColor(getResources().getColor(R.color.black));
                                setClick(tv_right, true);
                            }

                        }

                        @Override
                        public void afterTextChanged(Editable editable) {
                        }
                    });

                } else {
                    iv_delete.setVisibility(View.INVISIBLE);

                }
            }
        });

    }

    private void setClick(TextView tv_right, boolean b) {
        tv_right.setClickable(b);
        tv_right.setFocusable(b);
        tv_right.setEnabled(b);
    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

            case R.id.tv_right://完成

                if (CommonUtils.isNull(et_name.getText().toString().trim())) {

                    showToast("姓名不能为空");
                    return;
                }

                upDateUserInfo();

                break;
            case R.id.iv_delete:
                et_name.setText("");

                InputMethodManager inputManager =
                        (InputMethodManager) et_name.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(et_name, 0);

                break;
        }
    }

    /**
     * 提交用户修改的信息
     */
    private void upDateUserInfo() {

        if (babyInfo==null) {
            showToast("数据错误");
            return;
        }

        JSONObject j = new JSONObject();
        j.put("id", id);
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("birthDay", babyInfo.birthDay);
        j.put("name", et_name.getText().toString().trim());
        j.put("sex", babyInfo.sex);
        j.put("photo", babyInfo.picUrl);

        HttpUtil.setPostRequest("更新中...", this, Constant.WEB_ROOT + Constant.UPDATECHILDINFO,
                j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {

                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            org.json.JSONObject result = new org.json.JSONObject(str);

                            if (result.getInt("code") == Constant.STATUS_SUCCESS) {

                                babyInfo.childName = et_name.getText().toString().trim();
                                EventBus.getDefault().post(babyInfo);
                                finish();

                            } else {

                                showToast(Constant.showErrorMsg(result.getInt("code"), BabyModifyNameActivity.this));

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast(Constant.NODATA);

                        }
                    }
                }, null);
    }
}
